package execxp

import (
	
	
	
	

	
	
	
	
	
	
	
	
)

type xpFilt struct {
	t         tree.Node
	ctx       tree.Result
	expr      pathexpr.PathExpr
	ns        map[string]string
	ctxPos    int
	ctxSize   int
	proxPos   map[int]int
	fns       map[xml.Name]tree.Wrap
	variables map[string]tree.Result
}

type xpExecFn func(*xpFilt, string)

var xpFns = map[lexer.XItemType]xpExecFn{
	lexer.XItemAbsLocPath:     xfAbsLocPath,
	lexer.XItemAbbrAbsLocPath: xfAbbrAbsLocPath,
	lexer.XItemRelLocPath:     xfRelLocPath,
	lexer.XItemAbbrRelLocPath: xfAbbrRelLocPath,
	lexer.XItemAxis:           xfAxis,
	lexer.XItemAbbrAxis:       xfAbbrAxis,
	lexer.XItemNCName:         xfNCName,
	lexer.XItemQName:          xfQName,
	lexer.XItemNodeType:       xfNodeType,
	lexer.XItemProcLit:        xfProcInstLit,
	lexer.XItemStrLit:         xfStrLit,
	lexer.XItemNumLit:         xfNumLit,
}

func xfExec( *xpFilt,  *parser.Node) ( error) {
	for  != nil {
		if ,  := xpFns[.Val.Typ];  {
			(, .Val.Val)
			 = .Left
		} else if .Val.Typ == lexer.XItemPredicate {
			if  = xfPredicate(, .Left);  != nil {
				return
			}

			 = .Right
		} else if .Val.Typ == lexer.XItemFunction {
			if  = xfFunction(, );  != nil {
				return
			}

			 = .Right
		} else if .Val.Typ == lexer.XItemOperator {
			 := xpFilt{
				t:         .t,
				ns:        .ns,
				ctx:       .ctx,
				ctxPos:    .ctxPos,
				ctxSize:   .ctxSize,
				proxPos:   .proxPos,
				fns:       .fns,
				variables: .variables,
			}
			,  := exec(&, .Left)
			if  != nil {
				return 
			}

			 := xpFilt{
				t:         .t,
				ns:        .ns,
				ctx:       .ctx,
				fns:       .fns,
				variables: .variables,
			}
			,  := exec(&, .Right)
			if  != nil {
				return 
			}

			return xfOperator(, , , .Val.Val)
		} else if .Val.Typ == lexer.XItemVariable {
			if ,  := .variables[.Val.Val];  {
				.ctx = 
				return nil
			}
			return fmt.Errorf("Invalid variable '%s'", .Val.Val)
		} else if string(.Val.Typ) == "" {
			 = .Left
			//} else {
			//	return fmt.Errorf("Cannot process " + string(n.Val.Typ))
		}
	}

	return
}

func xfPredicate( *xpFilt,  *parser.Node) ( error) {
	 := .ctx.(tree.NodeSet)
	 := make(tree.NodeSet, 0, len())

	for  := range  {
		 := xpFilt{
			t:         .t,
			ns:        .ns,
			ctxPos:    ,
			ctxSize:   .ctxSize,
			ctx:       tree.NodeSet{[]},
			fns:       .fns,
			variables: .variables,
		}

		,  := exec(&, )
		if  != nil {
			return 
		}

		,  := checkPredRes(, , [])
		if  != nil {
			return 
		}

		if  {
			 = append(, [])
		}
	}

	.proxPos = make(map[int]int)
	for ,  := range  {
		.proxPos[.Pos()] =  + 1
	}

	.ctx = 
	.ctxSize = len()

	return
}

func checkPredRes( tree.Result,  *xpFilt,  tree.Node) (bool, error) {
	if ,  := .(tree.Num);  {
		if float64(.proxPos[.Pos()]) == float64() {
			return true, nil
		}
		return false, nil
	}

	if ,  := .(tree.IsBool);  {
		return bool(.Bool()), nil
	}

	return false, fmt.Errorf("Cannot convert argument to boolean")
}

func xfFunction( *xpFilt,  *parser.Node) error {
	 := strings.Split(.Val.Val, ":")
	var  xml.Name
	if len() == 1 {
		.Local = [0]
	} else {
		.Space = .ns[[0]]
		.Local = [1]
	}
	,  := intfns.BuiltIn[]
	if ! {
		,  = .fns[]
	}

	if  {
		 := []tree.Result{}
		 := .Left

		for  != nil {
			 := xpFilt{
				t:         .t,
				ctx:       .ctx,
				ns:        .ns,
				ctxPos:    .ctxPos,
				ctxSize:   .ctxSize,
				fns:       .fns,
				variables: .variables,
			}
			,  := exec(&, .Left)
			if  != nil {
				return 
			}

			 = append(, )
			 = .Right
		}

		,  := .Call(tree.Ctx{NodeSet: .ctx.(tree.NodeSet), Size: .ctxSize, Pos: .ctxPos + 1}, ...)
		.ctx = 
		return 
	}

	return fmt.Errorf("Unknown function: %s", .Val.Val)
}

var eqOps = map[string]bool{
	"=":  true,
	"!=": true,
}

var booleanOps = map[string]bool{
	"=":  true,
	"!=": true,
	"<":  true,
	"<=": true,
	">":  true,
	">=": true,
}

var numOps = map[string]bool{
	"*":   true,
	"div": true,
	"mod": true,
	"+":   true,
	"-":   true,
	"=":   true,
	"!=":  true,
	"<":   true,
	"<=":  true,
	">":   true,
	">=":  true,
}

var andOrOps = map[string]bool{
	"and": true,
	"or":  true,
}

func xfOperator(,  tree.Result,  *xpFilt,  string) error {
	if booleanOps[] {
		,  := .(tree.NodeSet)
		,  := .(tree.NodeSet)
		if  &&  {
			return bothNodeOperator(, , , )
		}

		if  {
			return leftNodeOperator(, , , )
		}

		if  {
			return rightNodeOperator(, , , )
		}

		if eqOps[] {
			return equalsOperator(, , , )
		}
	}

	if numOps[] {
		return numberOperator(, , , )
	}

	if andOrOps[] {
		return andOrOperator(, , , )
	}

	//if op == "|" {
	return unionOperator(, , , )
	//}

	//return fmt.Errorf("Unknown operator " + op)
}

func xfAbsLocPath( *xpFilt,  string) {
	 := .t
	for .GetNodeType() != tree.NtRoot {
		 = .GetParent()
	}
	.ctx = tree.NodeSet{}
}

func xfAbbrAbsLocPath( *xpFilt,  string) {
	 := .t
	for .GetNodeType() != tree.NtRoot {
		 = .GetParent()
	}
	.ctx = tree.NodeSet{}
	.expr = abbrPathExpr()
	find()
}

func xfRelLocPath( *xpFilt,  string) {
}

func xfAbbrRelLocPath( *xpFilt,  string) {
	.expr = abbrPathExpr()
	find()
}

func xfAxis( *xpFilt,  string) {
	.expr.Axis = 
}

func xfAbbrAxis( *xpFilt,  string) {
	.expr.Axis = xconst.AxisAttribute
}

func xfNCName( *xpFilt,  string) {
	.expr.Name.Space = 
}

func xfQName( *xpFilt,  string) {
	.expr.Name.Local = 
	find()
}

func xfNodeType( *xpFilt,  string) {
	.expr.NodeType = 
	find()
}

func xfProcInstLit( *xpFilt,  string) {
	 := tree.NodeSet{}
	for ,  := range .ctx.(tree.NodeSet) {
		if .GetToken().(xml.ProcInst).Target ==  {
			 = append(, )
		}
	}
	.ctx = 
}

func xfStrLit( *xpFilt,  string) {
	.ctx = tree.String()
}

func xfNumLit( *xpFilt,  string) {
	,  := strconv.ParseFloat(, 64)
	.ctx = tree.Num()
}

func abbrPathExpr() pathexpr.PathExpr {
	return pathexpr.PathExpr{
		Name:     xml.Name{},
		Axis:     xconst.AxisDescendentOrSelf,
		NodeType: xconst.NodeTypeNode,
	}
}

func find( *xpFilt) {
	 := make(map[int]tree.Node)
	.proxPos = make(map[int]int)

	if .expr.Axis == "" && .expr.NodeType == "" && .expr.Name.Space == "" {
		if .expr.Name.Local == "." {
			.expr = pathexpr.PathExpr{
				Name:     xml.Name{},
				Axis:     xconst.AxisSelf,
				NodeType: xconst.NodeTypeNode,
			}
		}

		if .expr.Name.Local == ".." {
			.expr = pathexpr.PathExpr{
				Name:     xml.Name{},
				Axis:     xconst.AxisParent,
				NodeType: xconst.NodeTypeNode,
			}
		}
	}

	.expr.NS = .ns

	for ,  := range .ctx.(tree.NodeSet) {
		for ,  := range findutil.Find(, .expr) {
			[.Pos()] = 
			.proxPos[.Pos()] =  + 1
		}
	}

	 := make(tree.NodeSet, 0, len())
	for ,  := range  {
		 = append(, )
	}

	xsort.SortNodes()

	.expr = pathexpr.PathExpr{}
	.ctxSize = len()
	.ctx = 
}